<?php

/**
* @package		auto_occasion
* @subpackage 	recherche
* @version      1
* @author       CMS
*/


/**
* Zone de la page de recherche en front
*
* @package auto_occasion
* @subpackage commun
*/
class FoRechercheLeftZone extends jZone {
 
	protected $_tplname='recherche~FoRechercheLeft.zone';

	/**
	* Chargement des données pour affichage
	*/
	protected function _prepareTpl(){
        global $gJCoord;

        jClasses::inc('commun~common');
        jClasses::inc('commun~sessionSrv');
        jClasses::inc('commun~tagCloudSrv');
        jClasses::inc('marqueModele~marqueSrv');
        jClasses::inc('essais~essaisSrv') ;
        jClasses::inc('marqueModele~modeleSrv') ;
        jClasses::inc ("commun~CNavBar") ;
        jClasses::inc ("annonce~annonceSrv") ;
        jClasses::inc ("entreprise~entrepriseSrv") ;

        $iCurrentPage      = $gJCoord->request->getParam('iCurrentPage', 1);

        $critereExiste = false;

        $marqueId  = $gJCoord->request->getParam('marque', '');
        $modeleId  = $gJCoord->request->getParam('modele', '');
        $clean     = $gJCoord->request->getParam('clean', '');
        $version   = $gJCoord->request->getParam('version', '');
        $cat       = $gJCoord->request->getParam('cat', '');
        $carbu     = $gJCoord->request->getParam('carbu', '');
        $porte     = $gJCoord->request->getParam('porte', '');
        $annee     = $gJCoord->request->getParam('annee', '');
        $km        = $gJCoord->request->getParam('km', '');
        $vitesse   = $gJCoord->request->getParam('vitesse', '');
        $localisation   = $gJCoord->request->getParam('localisation', '');
        $dept      = $gJCoord->request->getParam('dept', '');
        $vendeur   = $gJCoord->request->getParam('vendeur', '');
        $image     = $gJCoord->request->getParam('image', '');
        $video     = $gJCoord->request->getParam('video', '');
        $main      = $gJCoord->request->getParam('main', '');
        $garantie  = $gJCoord->request->getParam('garantie', '');
        $motcle    = $gJCoord->request->getParam('motcle', '');
        $order     = $gJCoord->request->getParam('order', '');
        $sort_     = $gJCoord->request->getParam('sort', '');
        $region    = $gJCoord->request->getParam('region', '');
        $prix      = $gJCoord->request->getParam('prix', '');
        $ville     = $gJCoord->request->getParam('ville', '');
        $lastAdvert= $gJCoord->request->getParam('last', 0); // x dernieres annonce
        $fm        = $gJCoord->request->getParam('fm', 0); // recherche effectuée à partir de la fiche annonce soit region soit dpt
        $urn       = $gJCoord->request->getParam('urn', 'nan'); // clean mot cle

        // les mots clés se cumulent
        if (isset($_SESSION['SESS_ANNONCE_MOTCLE'])) 
            $motCleUrne = unserialize($_SESSION['SESS_ANNONCE_MOTCLE']);
        else
            $motCleUrne = array();
        
        // suppr critere version - nuage de tag
        if ($urn != 'nan') {
            unset($motCleUrne[$urn]);
            $motCleUrne = array_values($motCleUrne);
        }

        if ($motcle && !in_array($motcle, $motCleUrne)) {
            $motCleUrne[] = $motcle;
        }

        if (sizeof($motCleUrne)) 
            $_SESSION['SESS_ANNONCE_MOTCLE'] = serialize($motCleUrne);

        if(($sort_ == '' ) && (isset($_SESSION['SESS_ANNONCE_SEARCH']))){
            
           $sessionCritereTmp = unserialize($_SESSION['SESS_ANNONCE_SEARCH']); 
           
           if($order == ''){
               $sort_ = $sessionCritereTmp->sort_;
           } elseif($order == $sessionCritereTmp->order){
               $sort_ = ($sessionCritereTmp->sort_ != 'ASC') ? 'ASC' : 'DESC';
           } 
           unset($sessionCritereTmp);
        }
        $limit     = $gJCoord->request->getParam('limit', 500);
        $likeMarque= $gJCoord->request->getParam('lMarque','');
        $likeModele= $gJCoord->request->getParam('lModele','');

        // teste si un critère au moins existe
        if ($marqueId || $modeleId || $version || $cat || $carbu || $porte || $annee || $km || $vitesse || $localisation || $dept || $vendeur || $image || $video || $main || $garantie || $motcle || $region || $prix || $ville) {
            $critereExiste = true;
        }


        if (isset($_SESSION['SESS_ANNONCE_SEARCH'])) {

            $temp = unserialize($_SESSION['SESS_ANNONCE_SEARCH']);
        } elseif(isset($_COOKIE['critereRecherche']) && !$critereExiste) {
            $temp = unserialize($_COOKIE['critereRecherche']);
        } elseif($lastAdvert) {
            $temp = sessionSrv::init();
        } else {
            $temp = sessionSrv::init();
        }

        //suppression par critère
        if ($clean) {
            switch ($clean) {
                case 'mrk' : // marque
                    $temp->marque   = "";
                    $temp->marqueId = 0;
                    $temp->modele = "";
                    $temp->modeleId = 0;
                    break;
                case 'mod' : // modele
                    $temp->modele = "";
                    $temp->modeleId = 0;
                    break;
                case 'ver' : // version
                    $temp->version = "";
                    break;
                case 'cat' : // categorie
                    $temp->cat = "";
                    break;
                case 'carbu' : // carburation
                    $temp->carbu = "";
                    break;
                case 'porte' : // nb porte
                    $temp->porte = "";
                    break;
                case 'annee' : // annee
                    $temp->annee = "";
                    break;
                case 'km' : // kilometrage
                    $temp->km = "";
                    break;
                case 'vitesse' : // vitesse
                    $temp->vitesse = "";
                    break;
                case 'localisation' : // localisation
                    $temp->localisation = "";
                    $temp->dept = "";
                    break;
                case 'dept' : // dept
                    $temp->dept = "";
                    break;
                case 'vendeur' : // vendeur
                    $temp->vendeur = "";
                    break;
                case 'order' : // ordre
                    $temp->order = "";
                    break;
                case 'sort_' : // asc || desc
                    $temp->sort_ = "";
                    break;
                case 'limit' : // vendeur
                    $temp->limit = "";
                    break;
                 case 'motcle' : // nuage de tag
                    $temp->motcle = "";
                    break;
                 case 'image' : // image
                    $temp->image = "";
                    break;
                 case 'video' : // video
                    $temp->video = "";
                    break;
                 case 'main' : // main
                    $temp->main = "";
                    break;
                 case 'garantie' : // garantie
                    $temp->garantie = "";
                    break;
                 case 'region' : // region
                    $temp->region = "";
                    break;
                 case 'ville' : // ville
                    $temp->ville = "";
                    break;
                 case 'prix' : // prix
                    $temp->prix = "";
                    break;


                default:
                    unset($_SESSION['SESS_ANNONCE_SEARCH']); // all
                    $temp = sessionSrv::init();
            }
        }
        
        $marqueId        = ($marqueId) ? $marqueId :$temp->marqueId;
        $modeleId        = ($modeleId) ? $modeleId : $temp->modeleId;
        $marque          = ($marqueId) ? common::chargeParId($marqueId, 'ao_marque') : common::init('ao_marque');
        $allMarque       = marqueSrv::getAllMarque(true);
        $modele          = ($marqueId) ? modeleSrv::getAllModeleByMarqueId($marqueId)  : common::init('ao_modele');
        $categorie       = essaisSrv::getAllCatVoiture() ;
        $modeleCurrent   = ($modeleId) ? common::chargeParId($modeleId, 'ao_modele') : common::init('ao_modele');
        $catCurrent      = ($cat) ? common::chargeParId($cat, 'ao_categorie_voitures') : common::init('ao_categorie_voitures');
        $carbuCurrent    = ($carbu) ? common::chargeParId($carbu, 'ao_carburation') : common::init('ao_carburation');
        $vitesseCurrent  = ($vitesse) ? common::chargeParId($vitesse, 'ao_vitesse') : common::init('ao_vitesse');
        $vendeurCurrent  = ($vendeur == 1) ? "Particulier" : ($vendeur == 2) ? "Pro" : "";
        $deptCurrent     = ($dept)? entrepriseSrv::getDepartementNom($dept):'';
        $region          = ($region)? $region :$temp->region;
        $prix            = ($prix)? $prix : $temp->prix;
        $ville           = ($ville)? $ville : $temp->ville;
	

        $boiteVitesse = common::liste('ao_vitesse');

        $sessionCritere = sessionSrv::init();
        
        // mémorisation des critères
        $sessionCritere->marqueId     = $marqueId;
        $sessionCritere->modeleId     = $modeleId;
        $sessionCritere->marque       = ($marque->marque_libelle) ? $marque->marque_libelle : $temp->marque ;
        $sessionCritere->modele       = ($modeleCurrent->modele_libelle) ? $modeleCurrent->modele_libelle : $temp->modele ;
        $sessionCritere->version      = ($version) ? $version : $temp->version ;
        $sessionCritere->cat          = ($catCurrent->catV_zLibelle) ? $catCurrent->catV_zLibelle: $temp->cat;
        $sessionCritere->carbu        = ($carbu) ? $carbuCurrent->name: $temp->carbu;
        $sessionCritere->porte        = ($porte) ? $porte: $temp->porte;
        $sessionCritere->annee        = ($annee) ? $annee: $temp->annee;
        $sessionCritere->km           = ($km) ? $km: $temp->km;
        $sessionCritere->vitesse      = ($vitesseCurrent->name) ? $vitesseCurrent->name: $temp->vitesse;
        $sessionCritere->vitesseId    = ($vitesseCurrent->id) ? $vitesseCurrent->id: 0;
        $sessionCritere->localisation = ($localisation) ? $localisation : $temp->localisation;
        $sessionCritere->vendeur      = ($vendeurCurrent) ? $vendeurCurrent : $temp->vendeur;
        $sessionCritere->image        = ($image) ? $image : $temp->image;
        $sessionCritere->video        = ($video) ? $video : $temp->video;
        $sessionCritere->main         = ($main) ? $main : $temp->main;
        $sessionCritere->garantie     = ($garantie) ? $garantie : $temp->garantie;
        $sessionCritere->motcle       = (isset($motcle)) ? $motcle : $temp->motcle;
        $sessionCritere->order        = ($order) ? $order : $temp->order;
        $sessionCritere->limit        = ($limit) ? $limit : $temp->limit;
        $sessionCritere->lMarque      = ($likeMarque) ? $likeMarque : $temp->lMarque;
        $sessionCritere->lModele      = ($likeModele) ? $likeModele : $temp->lModele;
        $sessionCritere->motCleUrne   = $motCleUrne;
        if ($temp->dept)
            $sessionCritere->departement  = ($deptCurrent) ? $deptCurrent : entrepriseSrv::getDepartementNom($temp->dept);
        else 
            $sessionCritere->departement  = ($deptCurrent) ? $deptCurrent : '';
        $sessionCritere->dept         = ($dept) ? $dept : $temp->dept;
        $sessionCritere->region       = ($region) ? $region : $temp->region;
        $sessionCritere->prix         = ($prix) ? $prix : $temp->prix;
        $sessionCritere->ville        = ($ville) ? $ville : $temp->ville;


        $sessionCritere->sort_    = (isset($sort_) && ($sort_ != '')) ? $sort_ : $temp->sort_;

        if  ($sessionCritere->dept && $fm == 2){
                 $sessionCritere->region   = '';
        }
        if  ($sessionCritere->region && $fm == 1){
                 $sessionCritere->dept   = '';
        }


        $_SESSION['SESS_ANNONCE_SEARCH'] = serialize($sessionCritere);
        $critereOk = false;
        $critereOk = $critereOk || ($sessionCritere->marqueId != 0);
        $critereOk = $critereOk || ($sessionCritere->modeleId != 0);
        $critereOk = $critereOk || ($sessionCritere->marque != '');
        $critereOk = $critereOk || ($sessionCritere->modele != '');
        $critereOk = $critereOk || ($sessionCritere->version != '');
        $critereOk = $critereOk || ($sessionCritere->cat != '');
        $critereOk = $critereOk || ($sessionCritere->carbu != '');
        $critereOk = $critereOk || ($sessionCritere->porte != '');
        $critereOk = $critereOk || ($sessionCritere->annee != '');
        $critereOk = $critereOk || ($sessionCritere->km != '');
        $critereOk = $critereOk || ($sessionCritere->vitesse != '');
        $critereOk = $critereOk || ($sessionCritere->vitesseId != 0);
        $critereOk = $critereOk || ($sessionCritere->localisation != '');
        $critereOk = $critereOk || ($sessionCritere->dept != '');
        $critereOk = $critereOk || ($sessionCritere->vendeur != '');
        $critereOk = $critereOk || ($sessionCritere->image != '');
        $critereOk = $critereOk || ($sessionCritere->video != '');
        $critereOk = $critereOk || ($sessionCritere->main != '');
        $critereOk = $critereOk || ($sessionCritere->garantie != '');
        $critereOk = $critereOk || ($sessionCritere->motcle != '');
        $critereOk = $critereOk || ($sessionCritere->garantie != '');
        $critereOk = $critereOk || ($sessionCritere->lMarque != '');
        $critereOk = $critereOk || ($sessionCritere->lModele != '');
        $critereOk = $critereOk || ($sessionCritere->prix != '');
        $critereOk = $critereOk || ($sessionCritere->region != '');
        $critereOk = $critereOk || ($sessionCritere->ville != '');
        $critereOk = $critereOk || (sizeof($motCleUrne));
        
        $autresCritereExiste = false;
        if(($sessionCritere->image == '') ||  ($sessionCritere->video == '') || ($sessionCritere->main == '') || ($sessionCritere->garantie == '') )
            $autresCritereExiste = true;

        // recherche
        // ---- pagination

        if ($iCurrentPage> 1){
            $iDebutListe = ($iCurrentPage-1) * ANNONCE_NBITEMPARPAGE; 
        }else{
            $iDebutListe = 0 ;
        }

        $xAnnonceParPage = parametre::get(X_LAST_ADVERT);

        if ($lastAdvert) { 
            $parPage = $xAnnonceParPage->param_content;
            $sessionCritere->doIt = 1;
        } else {
            $parPage = 20;
            $sessionCritere->doIt = 0;
            if($limit == 40 || $limit == 20) {
                $parPage = $limit;
                unset($sessionCritere->limit);
            }
        }
        if ($lastAdvert) $sessionCritere->limit = $xAnnonceParPage->param_content;
        $oNavBar = new CNavBar ($parPage, 7) ;
        $oNavBar->iCurrPage = $iCurrentPage;
        $annonce = annonceSrv::search($sessionCritere, $iDebutListe, $parPage) ;

        $_SESSION['SESS_ANNONCE_RESULT'] = serialize($annonce);

        // création du cookie
        $expire = 60*60*24*30; // 30J
        setcookie("critereRecherche", serialize($sessionCritere), time()+(int)$expire);		

        $oNavBar->normalizeBar ($annonce['nbRecords']) ;
        $oNavBar->mergeBar ();

        $_SESSION['SESS_ANNONCE_NAVBAR'] = serialize($oNavBar);
        // Récupération des version et des départements
        $version = $ids = array();
        if (sizeof($annonce['annonce'])) {
            foreach ($annonce['annonce'] as $item)  {
                $version[]  = $item->Occas_Version;
                $ids[]      = $item->IdOccasion;
            }
        }

        $_SESSION['SESS_ANNONCE_IDS'] = serialize($ids);

        // Nuage de version
        // remove selected version from mot clé
        if (sizeof($version) && sizeof($motCleUrne)) {
            $motCleUrne = array_map("strtolower", $motCleUrne);
            $version    = array_map("strtolower", $version);
            $temp = array();
            foreach ($version as $k=>$itemVer) {
                foreach ($motCleUrne as $itemUrne) {
                    $itemVer = str_replace($itemUrne, '', $itemVer);
                }
                $temp[]  = $itemVer;
            }
            $version = $temp;
        }
        $text_content = (sizeof($version))? implode(' ',  $version) : "";
        
        // nbre d'éléments à afficher dans le cloud : 50
        $nuageDeTag = new tagCloudSrv(50);
        $nuageDeTag->addTagsFromText($text_content);
        $nuageDeTag->setWidth("191px");

        $this->_tpl->assign("nuageDeTag", $nuageDeTag->emitCloud());
        $this->_tpl->assign("marque", $marque);
        $this->_tpl->assign("modele", $modele);
        $this->_tpl->assign("boiteVitesse", $boiteVitesse);
        $this->_tpl->assign("sessionCritere", $sessionCritere);
        $this->_tpl->assign("critereOk", $critereOk);
        $this->_tpl->assign("autresCritereExiste", $autresCritereExiste);
        $this->_tpl->assign("categorie", $categorie->fetchAll());
        $this->_tpl->assign("annee", date("Y"));
        $this->_tpl->assign("allMarque", $allMarque);

    }
}